﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_PGJGBMInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            YZZZ();
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.OnLoadData += new EventHandler(CustomGridView1_OnLoadData);
            if (!base.Page.IsPostBack) {
                BindData();
                string sql = "Select rolename from zs_role where id in (Select roleid From zs_user_in_role Where userid = '" + userid + "' and (rolename like '%评估机构%' or rolename like '%市征收%'  or rolename like '%超级管理员%' ) )";
                DataTable dt = _EluZSDataProvider.GetTable(sql);
                if (dt.Rows.Count>0)
                {
                    CustomGridView1.Columns[9].Visible = true;
                    CustomGridView1.Columns[10].Visible = true;
                }
                //if (!string.IsNullOrEmpty(temp))
                //{
                //    if (temp == "评估机构")
                //    {
                //        CustomGridView1.Columns[9].Visible = true;
                //        CustomGridView1.Columns[10].Visible = true;
                //    }
                //}
            }
            foreach (GridViewRow gvr in CustomGridView1.Rows) {
                Button btn_tj1 = (Button)gvr.FindControl("btn_tj1");
                btn_tj1.Click += new EventHandler(ChangeStatus1);
                isbm(btn_tj1);

            }




           
        }

        //是否报名
        protected void isbm(Button btn_tj1)
        {
            string sql = @"Select *
  From Zs_Pgbmjl
 Where Comid In
       (Select Id
          From Zs_Company
         Where 单位名称 Like
               (Select Name
                  From Zs_User
                 Where Id = '" + userid + "')) And Pnid = '" + Page.Request["pnid"] + "'";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count>0)
            {
                btn_tj1.Enabled = false;

            }
        }


        private void YZZZ()
        {
            string sql = @"Select 房地产估价机构资质等级 From zs_company Where 单位名称 = 
                        (Select Name From zs_user Where Id = '" + userid + "') And 入围年度 = '" + System.DateTime.Now.Year + "'";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                string zzdj = dt.Rows[0]["房地产估价机构资质等级"].ToString();
                string sql1 = "Select 资质 From zs_pgfbsq Where pnid = '" + base.Page.Request["PNID"] + "'";
                string zzdj1 = _EluZSDataProvider.GetString(sql1);
                if (zzdj1.Equals("乙级") && zzdj.Equals("一级"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "3", "<script>alert('该项目为甲级项目，与贵公司资质不符');location.href = ('ZS_PGJGBM.aspx');</script>");
                }
            }
        }

        protected string ckComType()
        {
            string dwlb = string.Empty;
            if (!string.IsNullOrEmpty(userid))
            {   string cid = _EluZSDataProvider.GetSomeStr("zs_user_company", "cid", "userid", userid);
                if (!string.IsNullOrEmpty(cid)) { dwlb = _EluZSDataProvider.GetSomeStr("zs_company", "单位类别", "id", cid); }
            } return dwlb;
        }
        protected void btnSeach_Click(object sender, EventArgs e)
        {
            BindData();
        }
        public void BindData()
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            CustomGridView1.RowCreated += new GridViewRowEventHandler(CustomGridView1_RowCreated);
            CustomGridView1.RowDataBound += new GridViewRowEventHandler(CustomGridView1_RowDataBound);

            string sql = string.Format(@"select t.id,t.pnid,t.申请单位,t.经办人,to_char(t.申请时间,'yyyy-mm-dd') 申请时间,
(select 立项号 from zs_project where id in '{1}' ) 立项号,
(select 项目名称 from zs_project where id in '{1}') 项目名称,
nvl((Select distinct 报名状态 From Zs_Pgbmjl Where sid In t.id And Comid = (Select Id
                    From Zs_Company
                   Where 单位名称 =
                         (Select Name
                            From Zs_User
                           Where Id = '{2}')
                     And  入围年度 = '" + System.DateTime.Now.Year + @"')   ),0) 报名状态,
       nvl((Select distinct 通过状态 From Zs_Pgbmjl Where sid In t.id And Comid = (Select Id
                    From Zs_Company
                   Where 单位名称 =
                         (Select Name
                            From Zs_User
                           Where Id = '{2}')
                     And  入围年度 = '" + System.DateTime.Now.Year + @"')  ),0) 通过状态,
       nvl((Select distinct 审批状态 From Zs_Pgbmjl Where sid In t.id And Comid = (Select Id
                    From Zs_Company
                   Where 单位名称 =
                         (Select Name
                            From Zs_User
                           Where Id = '{2}')
                     And  入围年度 = '" + System.DateTime.Now.Year + @"')  ),0) 审批状态,
(select id bmid from ZS_PGBMFBXX where sid=t.id) bmid   
from zs_pgfbsq t Where 1=1 And 提交状态=1 and t.pnid ='{1}'
", GetStrWhere1(), base.Page.Request["PNID"],userid);
            sql += GetStrWhere2();
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.DataKeyNames = new string[] { "ID", "报名状态","通过状态","审批状态" };
            CustomGridView1.DataBind();
        }
        protected string GetStrWhere1() {
            string hqcid = _EluZSDataProvider.GetSomeStr("zs_user_company", "cid", "userid", userid);
            string strwhere = string.Empty;
            if (!string.IsNullOrEmpty(hqcid)) { strwhere = " and COMID='" + hqcid + "'"; }
            return strwhere;
        }
        protected string GetStrWhere2() {
            string sqlWhere = string.Empty;
            TextBox txtCName = (TextBox)FindControl("txtCName");
            TextBox txtJBR = (TextBox)FindControl("txtJBR");
            string strCName = txtCName.Text;
            string strHBR = txtJBR.Text;
            if (!string.IsNullOrEmpty(strCName))
            {
                sqlWhere += " and t.申请单位 like '%" + strCName + "%'";
            }
            if (!string.IsNullOrEmpty(strHBR))
            {
                sqlWhere += " and t.经办人 like '%" + strHBR + "%'";
            }
            return sqlWhere;
        }
        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
        protected void CustomGridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            foreach (GridViewRow gvr in CustomGridView1.Rows)
            {
                Button btn1 = (Button)gvr.FindControl("btn_tj1");
                Label lbl_bmzt = (Label)gvr.FindControl("lbl_bmzt");
                if (CustomGridView1.DataKeys[gvr.RowIndex].Values["报名状态"].ToString() == "0")
                {
                    lbl_bmzt.Text = "未报名";
                    btn1 = (Button)gvr.FindControl("btn_tj1");
                    string sid = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
                    string tt = _EluZSDataProvider.GetSomeStr("zs_pgbmfbxx", "count(*)", "sid", sid);


                    if (!string.IsNullOrEmpty(tt))
                    {
                        if (Convert.ToInt32(tt) == 0)
                        { btn1.Enabled = false; }
                        else
                        {
                            btn1.Enabled = true;
                        }
                    }
                }
                if (CustomGridView1.DataKeys[gvr.RowIndex].Values["报名状态"].ToString() == "1" && CustomGridView1.DataKeys[gvr.RowIndex].Values["通过状态"].ToString() == "0")
                {
                    lbl_bmzt.Text = "已报名";
                    btn1 = (Button)gvr.FindControl("btn_tj1");
                    string sid = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
                    string tt = _EluZSDataProvider.GetSomeStr("zs_pgbmfbxx", "count(*)", "sid", sid);


                    if (!string.IsNullOrEmpty(tt))
                    {
                        if (Convert.ToInt32(tt) == 0)
                        {
                            btn1.Enabled = false;
                        }
                        else
                        {
                            btn1.Enabled = true;
                        }
                    }
                }
                if (CustomGridView1.DataKeys[gvr.RowIndex].Values["报名状态"].ToString() == "1" && CustomGridView1.DataKeys[gvr.RowIndex].Values["通过状态"].ToString() == "1")
                {
                    lbl_bmzt.Text = "报名已通过";
                    btn1 = (Button)gvr.FindControl("btn_tj1");
                    string sid = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
                    string tt = _EluZSDataProvider.GetSomeStr("zs_pgbmfbxx", "count(*)", "sid", sid);
                    string sql = "Select * from ZS_PGBMJL where pnid = '" + sid + "'";
                   
                    if (!string.IsNullOrEmpty(tt))
                    {
                        if (Convert.ToInt32(tt) == 0)
                        { btn1.Enabled = false; }
                        else
                        { btn1.Enabled = true; }
                    }
                }




                string sid1 = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
                string sql1 = "Select * from ZS_PGBMJL where sid = '" + sid1 + "'";
                DataTable dt = _EluZSDataProvider.GetTable(sql1);
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["通过状态"].ToString() =="1")
                    {
                        btn1.Enabled = false;
                    }
                }
            }
          
        }
        protected void CustomGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string sID = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
                string tt = _EluZSDataProvider.GetSomeStr("zs_pgbmfbxx", "count(*)", "sid", sID);
                if (!string.IsNullOrEmpty(tt)) { if (Convert.ToInt32(tt) == 0) { CustomGridView1.Columns[8].Visible = false; } }
            }
        }
        ///报名按钮
        protected void ChangeStatus1(object sender, EventArgs e)
        {
            CustomGridView CustomGridView1 = (CustomGridView)FindControl("CustomGridView1");
            Button btn = sender as Button;
            GridViewRow gvr = btn.Parent.Parent as GridViewRow;
            string sID = CustomGridView1.DataKeys[gvr.RowIndex].Values["ID"].ToString();
            string cID = _EluZSDataProvider.GetSomeStr("zs_user_company", "CID", "userid", userid);
            if (!string.IsNullOrEmpty(cID))
            {
                _EluZSDataProvider.InsertPGJGBMXX(base.Page.Request["PNID"], cID, sID);
                //BindData();
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert1", "<script>alert('报名成功！');location.href=('zs_pgjgbminfo.aspx?pnid=" + base.Page.Request["PNID"] + "');</script>");
            }
            else
            {
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('当前登录用户未关联公司！');</script>");
            }

        }
    }
}


